package com.hdaccp.ch07

import com.hdaccp.log.{DayCityVideoAccessStat, StatDAO}
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.functions._

import scala.collection.mutable.ListBuffer
object Demo3 {
  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder()
      .master("local[2]")
      .appName("ch07Demo3App")
      .getOrCreate()
  import spark.implicits._

  val ds = spark.read.format("parquet").load("F:/accp教学/sparkresources/clean7")

    ds.select(ds.col("singer"),ds.col("gender")).groupBy("singer","gender").agg(count("gender").as("times")).show()

//    ds.createOrReplaceTempView("music")


  //  val df = ds.sqlContext.sql("select singer,gender,count(1) as times from music group by singer,gender order by count(1) desc") //.show()

   // val list = new ListBuffer[Mu]
   /* df.foreach(m =>{
      var singer = m.getAs[String]("singer")
      var gender = m.getAs[String]("gender")
      var times = m.getAs[Long]("times")
      println(singer + "--" +gender +"--" + times)
      //写入数据库
     // list.append(Mu(singer, gender, times))

      StatDAO.insertMu(singer,gender,times)
    })*/


   /* df.foreachPartition(p =>{
      val list = new  ListBuffer[Mu]
      p.foreach(m=>{
        var singer = m.getAs[String]("singer")
        var gender = m.getAs[String]("gender")
        var times = m.getAs[Long]("times")
        list.append(Mu(singer, gender, times))
      })

      StatDAO.insertMusic(list)
    })*/






    spark.stop()
  }
}
